use std::any::Any;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::Instant;
use log::info;
use rpc::session::RpcSession;

#[test]
fn session_test() {
    let mut session = RpcSession::test_session();
    session.add_data("t1".to_string(), Box::new("hello".to_string()));
    let result = session.with_data::<String, i32>("t1", |b| {
        info!("t1 value: {}", b);
        12
    });
    assert_eq!(result.is_some(), true);
}


#[test]
fn session_test3() {
    let mut session = RpcSession::test_session();
    session.add_data("t1".to_string(), Box::new("hello"));
    let result = session.with_data::<&'static str, i32>("t1", |b| {
        info!("t1 value: {}", b);
        12
    });
    assert_eq!(result.is_some(), true);
}

#[test]
fn session_test2() {
    let map :Arc<RwLock<HashMap<String, SessionDataItem>>> = Arc::new(RwLock::new(HashMap::new()));

    let k3 = "k3".to_string();
    let v3 = SessionDataItem{data: Box::new(12), expired_time: Instant::now()};
    map.write().unwrap().insert(k3.clone(), v3);

    {
        let lock = map.read().unwrap();
        let value = lock.get(&k3);
        if let Some(value) = value {
            let data = &value.data;
            if let Some(value) = data.downcast_ref::<i32>() {
                info!("v3 : {}", value);
            } else {
                info!("v3 is null");
            }
        }
    }
}

struct SessionDataItem {
    data: Box<dyn Any>,
    expired_time: Instant, // 过期时间， 默认100年后过期
}